package com.lear.mypush;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	private String mDeviceID;
	private Button startButton;
	private Button stopButton;
	private Button sendButton;
	private SharedPreferences mPrefs;
	private Intent serviceIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mPrefs = getSharedPreferences("LearPushPrefs", MODE_PRIVATE);
		serviceIntent = new Intent(MainActivity.this, StartupService.class);
		serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		mDeviceID = mPrefs.getString(PushService.PREF_DEVICE_ID, "client"
				+ (int) (Math.random() * 100));
		((TextView) findViewById(R.id.target_text)).setText(mDeviceID);
		mPrefs.edit().putString(PushService.PREF_DEVICE_ID, mDeviceID).commit();

		startButton = ((Button) findViewById(R.id.start_button));
		stopButton = ((Button) findViewById(R.id.stop_button));
		sendButton = ((Button) findViewById(R.id.btn_send));
		startButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PushService.actionStart(getApplicationContext());
				startButton.setEnabled(false);
				stopButton.setEnabled(true);
			}
		});
		stopButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				PushService.actionStop(getApplicationContext());
				startButton.setEnabled(true);
				stopButton.setEnabled(false);
			}
		});
		sendButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(PushService.SEND_BC);
				String send = ((EditText) findViewById(R.id.ed_input))
						.getText().toString();
				send = send.length() == 0 ? "Push Message at: "
						+ getTimeAndDate() : send;
				intent.putExtra("send_string", send);
				sendBroadcast(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	@Override
	protected void onResume() {
		super.onResume();
		// new PushThread().start();

		boolean started = mPrefs.getBoolean(PushService.PREF_STARTED, false);
		((Button) findViewById(R.id.start_button)).setEnabled(!started);
		((Button) findViewById(R.id.stop_button)).setEnabled(started);

	}

	@Override
	protected void onStop() {
		super.onStop();
		// stopService(serviceIntent);
	}

	private class PushThread extends Thread {
		public void run() {
			Log.e("HOME", "Start to begin push thread!");
			try {
				Thread.sleep(30 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			PushService.actionStart(getApplicationContext());
		}
	}

	public static String getTimeAndDate() {
		SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = dateFormatter.format(new Date());
		SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm");
		String timeString = timeFormatter.format(new Date());
		return dateString + " " + timeString;
	}
}
